﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CI.Models.Production;
using CI.Models.Production.Interfaces;

namespace CI.Models
{
    public abstract class JobBase : ModelBase, IJob
    {
        private IJob _parentJob;
        private ObservableCollection<IJob> _childJobs;
        private IQuantityCalculator<decimal> _cost;
        private IQuantityCalculator<TimeSpan> _time;
        private Guid _id;
        private decimal _setupCost;
        private bool _isPinned;
        private Activity _activity;
        private IOwner _owner;

        public Guid ID
        {
            get { return _id; }
            private set { SetField(ref _id, value, () => ID); }
        }

        public IJob ParentJob
        {
            get { return _parentJob; }
            internal set { SetField(ref _parentJob, value, () => ParentJob); }
        }

        public ObservableCollection<IJob> ChildJobs
        {
            get { return _childJobs; }
            private set { SetField(ref _childJobs, value, () => ChildJobs); }
        }

        public IQuantityCalculator<decimal> Cost
        {
            get { return _cost; }
            internal set { SetField(ref _cost, value, () => Cost); }
        }

        public IQuantityCalculator<TimeSpan> Time
        {
            get { return _time; }
            internal set { SetField(ref _time, value, () => Time); }
        }

        public bool IsPinned
        {
            get { return _isPinned; }
            set { SetField(ref _isPinned, value, () => IsPinned); }
        }

        public decimal SetupCost
        {
            get { return _setupCost; }
            internal set { SetField(ref _setupCost, value, () => SetupCost); }
        }

        public IOwner Owner
        {
            get { return _owner; }
            set { SetField(ref _owner, value, () => Owner); }
        }

        public Activity Activity
        {
            get { return _activity; }
            internal set { SetField(ref _activity, value, () => Activity); }
        }

        public IEnumerable<T> GetChildJobs<T>()
        {
            return ChildJobs.Where(w => typeof (T).IsAssignableFrom(w.GetType())).Cast<T>();
        }

        public JobBase()
        {
            ChildJobs = new ObservableCollection<IJob>();
            ID = Guid.NewGuid();
        }
    }
}